<template>
    <div class="add-product-modal-wrapper">
        <Block>
            <DataGrid :auto-load="autoLoad" ref="dataGridRef">
                <template #toolbar-search>
                    <div class="query-from-box">
                        <el-form>
                            <el-form-item>
                                <el-input v-model="queryData.messageTypeKey" placeholder="消息名称/标识符" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-select placeholder="消息性质" v-model="queryData.messageMethod" clearable>
                                    <el-option
                                        v-for="(value, key) in MessageMethod"
                                        :label="value"
                                        :key="key"
                                        :value="key"
                                    />
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-select placeholder="状态" v-model="queryData.enable" clearable>
                                    <el-option v-for="(value, key) in Enable" :label="value" :key="key" :value="key" />
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </div>
                </template>
                <el-table-column type="index" label="序号" width="60" align="center" fixed />
                <el-table-column label="消息类型" width="300" fixed show-overflow-tooltip>
                    <template #default="scope">
                        <el-button link type="primary" @click="onViewItem(scope.row)">
                            {{ scope.row.typeName }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="typeNo" label="标识符" show-overflow-tooltip />
                <el-table-column label="消息性质" show-overflow-tooltip>
                    <template #default="scope">
                        <div class="table-item-tag">
                            <el-tag :type="scope.row.messageMethod === 'SUBSCRIBE' ? 'primary' : 'success'">
                                {{ MessageMethod[scope.row.messageMethod] }}
                            </el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="topicRule" label="Topic生成规则" show-overflow-tooltip />
                <el-table-column label="状态" width="80">
                    <template #default="scope">
                        <span class="table-item-status" :status="Enable[scope.row.enable]">
                            {{ Enable[scope.row.enable] }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="80">
                    <template #default="scope">
                        <el-button link type="primary" v-if="scope.row.enable" @click="onDisable(scope.row)">
                            停用
                        </el-button>
                        <el-button link type="primary" v-else @click="onEnable(scope.row)"> 启用 </el-button>
                    </template>
                </el-table-column>
            </DataGrid>
        </Block>
    </div>
</template>
<script setup lang="ts">
import { messageClassifyService } from '@great/services';
import { reactive, ref } from 'vue';
import { DataGrid, Block, type DataGridInstance } from '@great/components';
import { showPopupBox } from '@great/utils';
import ViewItemModal from './components/list/ViewItemModal.vue';

enum Enable {
    true = '启用',
    false = '停用'
}
enum MessageMethod {
    SUBSCRIBE = '消费',
    PUBLISH = '生产'
}
// 查询条件
const queryData = ref({
    messageTypeKey: '',
    messageMethod: '',
    enable: ''
});

const dataGridRef = useTemplateRef<DataGridInstance>('dataGridRef');
const autoLoad = reactive({
    debounce: true,
    reloadDeps: [queryData],
    onLoad: async (current, size) => {
        const res = await messageClassifyService.getPage({
            current,
            size,
            ...queryData.value
        });
        return { data: res.data.records, total: res.data.total };
    }
});

// 查看详情
const onViewItem = (row: { id: string }) => {
    showPopupBox(ViewItemModal, {
        row: row
    });
};
// 停用
const onDisable = async (row: { typeName: string; typeNo: string }) => {
    ElMessageBox.confirm(`确认启用${row ? row.typeName : ''}消息类型？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            await messageClassifyService.disable(row.typeNo);
            ElNotification({
                title: '停用成功！',
                type: 'success'
            });
            dataGridRef.value?.refresh();
        })
        .catch(() => {});
};
// 启用
const onEnable = async (row: { typeName: string; typeNo: string }) => {
    ElMessageBox.confirm(`确认启用${row ? row.typeName : ''}消息类型？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            await messageClassifyService.enable(row.typeNo);
            ElNotification({
                title: '启用成功！',
                type: 'success'
            });
            dataGridRef.value?.refresh();
        })
        .catch(() => {});
};
</script>
<style scoped lang="scss">
.add-product-modal-wrapper {
    .query-from-box {
        display: flex;
        align-items: center;
        .el-form {
            display: flex;
            .el-form-item {
                margin-bottom: 0;
                width: 192px;
            }
            .el-form-item + .el-form-item {
                margin-left: 10px;
            }
        }
    }
    :deep(.data-grid) {
        .table-item-tag {
            display: flex;
        }
        .table-item-status {
            display: flex;
            align-items: center;
            &::before {
                content: '';
                width: 6px;
                height: 6px;
                display: inline-block;
                border-radius: 50%;
                margin-right: 5px;
            }
        }
        .table-item-status[status='停用'] {
            &::before {
                background-color: #909399;
            }
        }
        .table-item-status[status='启用'] {
            &::before {
                background-color: #67c23a;
            }
        }
    }
}
</style>
